﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace lyAdmin.Common
{
    /// <summary>
    /// Http请求
    /// </summary>
    public class HttpClientHelper
    {
        /// <summary>
        /// Post提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Post<T>(string url,T data)
        { 
            HttpClient httpClient = new HttpClient(); 
            var todoItemJson = new StringContent(
                JsonSerializer.Serialize(data, typeof(T)),
                Encoding.UTF8,
                "application/json");
            string html = httpClient.PostAsync(url, todoItemJson).Result.Content.ReadAsStringAsync().Result;
            Console.WriteLine(html);
            return html;
        }

        /// <summary>
        /// Get提交
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Get(string url)
        {
            HttpClient httpClient = new HttpClient(); 
            string html = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            Console.WriteLine(html);
            return html;
        }
        /// <summary>
        /// Put提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Put<T>(string url, T data)
        {
            HttpClient httpClient = new HttpClient();
            var todoItemJson = new StringContent(
                JsonSerializer.Serialize(data, typeof(T)),
                Encoding.UTF8,
                "application/json");
            string html = httpClient.PutAsync(url, todoItemJson).Result.Content.ReadAsStringAsync().Result;
            Console.WriteLine(html);
            return html;
        }
        /// <summary>
        /// Delete提交
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Delete(string url)
        {
            HttpClient httpClient = new HttpClient();
            string html = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            Console.WriteLine(html);
            return html;
        }

    }
}
